Load packages

library(sf)
## Linking to GEOS 3.9.1, GDAL 3.4.3, PROJ 7.2.1; sf_use_s2() is TRUE
library(rgdal)
## Loading required package: sp
## Please note that rgdal will be retired by the end of 2023,
## plan transition to sf/stars/terra functions using GDAL and PROJ
## at your earliest convenience.
## 
## rgdal: version: 1.5-32, (SVN revision 1176)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 3.4.3, released 2022/04/22
## Path to GDAL shared files: C:/Users/diana/AppData/Local/R/win-library/4.2/rgdal/gdal
## GDAL binary built with GEOS: TRUE 
## Loaded PROJ runtime: Rel. 7.2.1, January 1st, 2021, [PJ_VERSION: 721]
## Path to PROJ shared files: C:/Users/diana/AppData/Local/R/win-library/4.2/rgdal/proj
## PROJ CDN enabled: FALSE
## Linking to sp version:1.5-0
## To mute warnings of possible GDAL/OSR exportToProj4() degradation,
## use options("rgdal_show_exportToProj4_warnings"="none") before loading sp or rgdal.
library(tidyverse)
## ── Attaching packages
## ───────────────────────────────────────
## tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6      ✔ purrr   0.3.4 
## ✔ tibble  3.1.8      ✔ dplyr   1.0.10
## ✔ tidyr   1.2.1      ✔ stringr 1.4.1 
## ✔ readr   2.1.3      ✔ forcats 0.5.2 
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(tmap)
library(tmaptools)
library(janitor)
## 
## Attaching package: 'janitor'
## 
## The following objects are masked from 'package:stats':
## 
##     chisq.test, fisher.test

Read dataset as df

df <- read_delim("HDR21-22_Composite_indices_complete_time_series.csv", delim=",")
## Rows: 206 Columns: 1008
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr    (4): iso3, country, hdicode, region
## dbl (1004): hdi_rank_2021, hdi_1990, hdi_1991, hdi_1992, hdi_1993, hdi_1994,...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

Data cleaning and adding new Gender Inequality Index Difference between 2010 & 2019 column (gii_diff)

gii <- df %>%
  clean_names() %>%
  select(iso3, country, gii_2010, gii_2019, gii_rank_2021) %>%
  slice(1:195) %>%
  mutate(., gii_diff=(gii_2019-gii_2010)) %>%
  na.omit(df)
gii
## # A tibble: 165 × 6
##    iso3  country              gii_2010 gii_2019 gii_rank_2021 gii_diff
##    <chr> <chr>                   <dbl>    <dbl>         <dbl>    <dbl>
##  1 AFG   Afghanistan             0.753    0.665           167  -0.088 
##  2 AGO   Angola                  0.551    0.537           136  -0.0140
##  3 ALB   Albania                 0.246    0.156            39  -0.09  
##  4 ARE   United Arab Emirates    0.175    0.056            11  -0.119 
##  5 ARG   Argentina               0.364    0.306            69  -0.058 
##  6 ARM   Armenia                 0.346    0.239            53  -0.107 
##  7 AUS   Australia               0.136    0.08             19  -0.056 
##  8 AUT   Austria                 0.111    0.054            12  -0.057 
##  9 AZE   Azerbaijan              0.338    0.314            70  -0.0240
## 10 BDI   Burundi                 0.54     0.507           127  -0.0330
## # … with 155 more rows

Read world countries spatial data (shapefile) as world

world <- st_read("World_Countries_(Generalized)/World_Countries__Generalized_.shp")
## Reading layer `World_Countries__Generalized_' from data source 
##   `C:\Users\diana\CASA\Term1\0005_GIS\Practical\week5\wk5exam1\gis_hw4\World_Countries_(Generalized)\World_Countries__Generalized_.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 251 features and 7 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -180 ymin: -89 xmax: 180 ymax: 83.6236
## Geodetic CRS:  WGS 84

Load the countrycode package and change the countrycode so that it can be joined

library(countrycode)
gii$iso = countrycode(gii$iso3,"iso3c","iso2c")

Join the csv data and shapefile

world
## Simple feature collection with 251 features and 7 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -180 ymin: -89 xmax: 180 ymax: 83.6236
## Geodetic CRS:  WGS 84
## First 10 features:
##    FID             COUNTRY ISO          COUNTRYAFF AFF_ISO   SHAPE_Leng
## 1    1         Afghanistan  AF         Afghanistan      AF   50.8032097
## 2    2             Albania  AL             Albania      AL    9.6250380
## 3    3             Algeria  DZ             Algeria      DZ   70.5400822
## 4    4      American Samoa  AS       United States      US    0.6001244
## 5    5             Andorra  AD             Andorra      AD    0.9373518
## 6    6              Angola  AO              Angola      AO   58.8734698
## 7    7            Anguilla  AI      United Kingdom      GB    0.4683887
## 8    8          Antarctica  AQ                <NA>    <NA> 1564.1441426
## 9    9 Antigua and Barbuda  AG Antigua and Barbuda      AG    1.1629014
## 10  10           Argentina  AR           Argentina      AR  138.7513483
##      SHAPE_Area                       geometry
## 1  6.256671e+01 MULTIPOLYGON (((61.27655 35...
## 2  3.073540e+00 MULTIPOLYGON (((19.57083 41...
## 3  2.135135e+02 MULTIPOLYGON (((4.603354 36...
## 4  1.371972e-02 MULTIPOLYGON (((-170.7439 -...
## 5  5.558517e-02 MULTIPOLYGON (((1.445836 42...
## 6  1.038131e+02 MULTIPOLYGON (((23.47611 -1...
## 7  7.797911e-03 MULTIPOLYGON (((-63.16778 1...
## 8  5.672928e+03 MULTIPOLYGON (((-180 -84.30...
## 9  4.562595e-02 MULTIPOLYGON (((-61.73806 1...
## 10 2.783090e+02 MULTIPOLYGON (((-71.85916 -...
joined_data <- world %>% 
  clean_names() %>%
  left_join(., 
            gii,
            by = c("iso" = "iso"))
print(joined_data)
## Simple feature collection with 251 features and 13 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -180 ymin: -89 xmax: 180 ymax: 83.6236
## Geodetic CRS:  WGS 84
## First 10 features:
##    fid           country.x iso          countryaff aff_iso   shape_leng
## 1    1         Afghanistan  AF         Afghanistan      AF   50.8032097
## 2    2             Albania  AL             Albania      AL    9.6250380
## 3    3             Algeria  DZ             Algeria      DZ   70.5400822
## 4    4      American Samoa  AS       United States      US    0.6001244
## 5    5             Andorra  AD             Andorra      AD    0.9373518
## 6    6              Angola  AO              Angola      AO   58.8734698
## 7    7            Anguilla  AI      United Kingdom      GB    0.4683887
## 8    8          Antarctica  AQ                <NA>    <NA> 1564.1441426
## 9    9 Antigua and Barbuda  AG Antigua and Barbuda      AG    1.1629014
## 10  10           Argentina  AR           Argentina      AR  138.7513483
##      shape_area iso3   country.y gii_2010 gii_2019 gii_rank_2021 gii_diff
## 1  6.256671e+01  AFG Afghanistan    0.753    0.665           167   -0.088
## 2  3.073540e+00  ALB     Albania    0.246    0.156            39   -0.090
## 3  2.135135e+02  DZA     Algeria    0.519    0.437           126   -0.082
## 4  1.371972e-02 <NA>        <NA>       NA       NA            NA       NA
## 5  5.558517e-02 <NA>        <NA>       NA       NA            NA       NA
## 6  1.038131e+02  AGO      Angola    0.551    0.537           136   -0.014
## 7  7.797911e-03 <NA>        <NA>       NA       NA            NA       NA
## 8  5.672928e+03 <NA>        <NA>       NA       NA            NA       NA
## 9  4.562595e-02 <NA>        <NA>       NA       NA            NA       NA
## 10 2.783090e+02  ARG   Argentina    0.364    0.306            69   -0.058
##                          geometry
## 1  MULTIPOLYGON (((61.27655 35...
## 2  MULTIPOLYGON (((19.57083 41...
## 3  MULTIPOLYGON (((4.603354 36...
## 4  MULTIPOLYGON (((-170.7439 -...
## 5  MULTIPOLYGON (((1.445836 42...
## 6  MULTIPOLYGON (((23.47611 -1...
## 7  MULTIPOLYGON (((-63.16778 1...
## 8  MULTIPOLYGON (((-180 -84.30...
## 9  MULTIPOLYGON (((-61.73806 1...
## 10 MULTIPOLYGON (((-71.85916 -...

Plot the Gender Inequality Difference 2010 & 2019 map

tm_shape(joined_data) + 
  tm_polygons("gii_diff", 
              style="pretty",
              palette="Blues",
              midpoint=NA,
              #title="Gender Inequality Difference 2010 & 2019",
              alpha = 0.7) + 
  tm_compass(position = c("left", "bottom"),type = "arrow") + 
  tm_scale_bar(position = c("left", "bottom")) +
  tm_layout(title = "Gender Inequality Difference 2010 & 2019", legend.position = c("right", "bottom")) +
  tmap_options(max.categories = 115)
## Scale bar set for latitude km and will be different at the top and bottom of the map.

Plot the Gender Inequality Difference 2010 & 2019 map_for wk4Exam

tmap_mode("plot")
## tmap mode set to plotting
breaks=c(-0.6,-0.03,-0.01, 0.01, 1, 50, 100, 150, 200) 

tm1 <- tm_shape(joined_data) + 
  tm_polygons("gii_2019",
              breaks=breaks,
               palette="PuOr")+
  tm_legend(show=FALSE)+
  tm_layout(frame=FALSE)+
  tm_credits("(a1: gii_2019)", position=c(0,0.85), size=1.5)


tm2 <- tm_shape(joined_data) + 
  tm_polygons("gii_2010", 
               breaks=breaks,
             palette="PuOr")+
  tm_legend(show=FALSE)+
  tm_layout(frame=FALSE)+
  tm_credits("(a2: gii_2010)", position=c(0,0.85), size=1.5)


tm3 <- tm_shape(joined_data) + 
  tm_polygons("gii_rank_2021", 
               breaks=breaks, 
              palette="PuOr")+
  tm_legend(show=FALSE)+
  tm_layout(frame=FALSE)+
  tm_credits("(a3: gii_rank_2021)", position=c(0,0.85), size=1.5)


legend <- tm_shape(joined_data) +
    tm_polygons("gii_diff",
                 breaks=breaks,midpoint = NA,
               palette="PuOr") +
    tm_scale_bar(position=c(-0.4,0.8), text.size=0.6)+
    tm_compass(north=0, position=c(0.65,0.6))+
    tm_layout(legend.only = TRUE, legend.position=c(0.2,0.25),asp=0.1)+
    tm_credits("Gender Inequality Difference 2010 & 2019", position=c(0.0,0.0))
  
t=tmap_arrange(tm1, tm2, tm3, legend, ncol=2)
print(t)
## Scale bar set for latitude km and will be different at the top and bottom of the map.

library(grid)
grid.newpage()

pushViewport(viewport(layout=grid.layout(2,2)))
print(tm1, vp=viewport(layout.pos.col=2, layout.pos.row=2, height=7))
print(tm2, vp=viewport(layout.pos.col=1, layout.pos.row=2, height=7))
print(tm3, vp=viewport(layout.pos.col=2, layout.pos.row=1, height=7))
print(legend, vp=viewport(layout.pos.col=1, layout.pos.row=1, height=7))
## Scale bar set for latitude km and will be different at the top and bottom of the map.

tmap_mode("view")
## tmap mode set to interactive viewing
breaks=c(0,10,20,30,50,60,70,80,90,100,110,120,130,140,150,160)
tm_shape(joined_data) + 
  tm_polygons("gii_rank_2021", breaks=breaks) 
## Warning: Values have found that are higher than the highest break